// pages/video/video.js
var app = getApp();
var api = require('../../api.js');
var is_loading_more = false;
var is_no_more = false;
var WxParse = require('../../wxParse/wxParse.js');
var util = require('../../utils/util.js');
Page({

  /**
   *
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    tabArr: {
      curHdIndex: 0,
      curBdIndex: 0
    },

    isShowToast: false,
    collect_loading: false,
    c_id: 0,
    img_list: [],
    content: '',
    comment_count: 0,
    goods_loading: -1,
    page: 1,
    // show:false,
    show_modal: false,

    flag: true,//打赏弹窗参数
    flag2: true,

    scrollTop: 600,
    scrollHeight: 300,
  },


  //打赏弹窗的显示与隐藏
  showpop: function () {
    this.setData({ flag: false })
  },
  //消失

  hidepop: function () {
    this.setData({ flag: true })
  },
  showpop2: function () {
    this.setData({ flag2: false })
  },
  //消失

  hidepop2: function () {
    this.setData({ flag2: true, othermoney: '' })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    is_loading_more = false;
    is_no_more = false;
    var id = options.id;
    if (options.scene) {
      id = decodeURIComponent(options.scene);
    }
    var page = this;
    var time = util.formatdate(new Date());
    page.setData({
      video_id: id,
      time: time,
      store: wx.getStorageSync('store')
    });
    // console.log(page);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var page = this;
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var page = this;
    wx.showLoading({
      title: '加载中',
    });
    var user_info = wx.getStorageSync('user_info');
    if (user_info.is_member == 1) {
      page.setData({
        show_modal: false,
      });
    }
    var video_id = page.data.video_id;
    app.request({
      url: api.user.video,
      data: { video_id: video_id },
      success: function (res) {
        if (res.code == 0) {
          var video = res.data.video;
          console.log(video);
          if (video.status == 1) {
            wx.showModal({
              title: '提示',
              content: '该内容已下架',
              showCancel: false,
              confirmText: '去首页',
              success: function (res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '/pages/index/index',
                  })
                }
              }
            })
          } else {
            var video = wx.createVideoContext('video');
            var content = res.data.video.detail + '<span> </span>'
            WxParse.wxParse("detail", "html", content, page, 5);
            page.setData({
              video: res.data.video,
              video_list: res.data.video_list,
              video_id: res.data.video.id,
              video_pay: res.data.video_pay
            });
            wx.setNavigationBarTitle({
              title: res.data.video.title,
            })
          }
        }
      },
      complete: function (res) {
        wx.hideLoading();
        console.log(res);
      }
    });
  },

  buyVideo: function () {
    var page = this;
    var video = page.data.video;
    wx.showModal({
      title: '提示',
      content: '是否购？',
      success: function (e) {
        if (e.confirm) {
          wx.showLoading({
            title: '提交中',
          })
          app.request({
            url: api.order.buylive,
            method: 'POST',
            data: {
              video_id: video.id,
              price: page.data.video_pay.price
            },
            success: function (res) {
              if (res.code == 0) {
                app.request({
                  url: api.order.get_pay_data,
                  method: 'POST',
                  data: {
                    order_id: res.data,
                    pay_type: 'WECHAT_PAY'
                  },
                  success: function (result) {
                    wx.hideLoading();
                    if (result.code == 0) {
                      var pay_data = result.data;
                      wx.requestPayment({
                        timeStamp: pay_data.timeStamp,
                        nonceStr: pay_data.nonceStr,
                        package: pay_data.package,
                        signType: pay_data.signType,
                        paySign: pay_data.paySign,
                        success: function (res) {
                          wx.showToast({
                            title: '订单支付成功',
                            icon: 'success'
                          });
                          setTimeout(function () {
                            wx.redirectTo({
                              url: '/pages/video-live/video-live?id=' + video.id,
                            })
                          }, 2000)
                        },
                        fail: function (res) {
                          wx.showToast({
                            title: '订单未支付',
                            image: '/images/icon-warning.png'
                          });
                        }
                      });
                    } else {
                      wx.showModal({
                        title: '提示',
                        content: result.msg,
                        showCancel: false
                      });
                    }
                  }
                });
              } else {
                wx.showModal({
                  title: '警告',
                  content: res.msg,
                  showCancel: false
                })
              }
            }
          });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

    var page = this;
    var old = wx.createVideoContext("video");
    old.seek(0);
    old.pause();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var video = this.data.video;
    var result = {
      title: video.title,
      path: "/pages/video/video?id=" + video.id,
      imageUrl: video.pic_url,
      success: function (res) {
        wx.showToast({
          title: '转发成功',
        });
      },
      fail: function (res) {
        // 转发失败
      }
    };
    return result;

  },

  play: function (options) {
    wx.redirectTo({
      url: '/pages/video/video?id=' + options.currentTarget.dataset.id,
    })
  },
  loadComment: function (id) {
    var page = this;
    is_loading_more = true;
    app.request({
      url: api.user.chat_list,
      data: {
        video_id: id,
        page: 1
      },
      success: function (res) {
        if (res.code == 0) {
          page.setData({
            comment_list: res.data.list,
            comment_count: res.data.row_count,
            page_count: res.data.page_count,
            cc_count: res.data.cc_count,
            scrollview: 'view' + res.data.cc_count
          });
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.msg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
        is_loading_more = false;
      }
    });
  },
  formSubmit:function(e){
    var page = this;
    var password = e.detail.value.password;
    if (password == '') {
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    var video = page.data.video;
    var video_id = page.data.video_id;
    if (password == video.password) {
      wx.showToast({
        title: '密码成功',
        icon: 'success'
      });
      setTimeout(function () {
        wx.redirectTo({
          url: '/pages/video-live/video-live?id=' + video_id,
        })
      }, 2000)
    } else {
      wx.showToast({
        title: '密码错误',
        image: '/images/icon-warning.png',
        duration: 2000
      })
    }
  }

})